<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('article_m');
        $this->load->model('articleCategory_m');
    }

    public function index($page = 1)
    {
        $get    = $this->input->get();
        $search = $where = [];
        if (isset($get['article_title']) && !empty($get['article_title'])) {
            $search['keyword'] = $keyword = $get['article_title'];
            $where             = "title like '%{$keyword}%'";
        }
        //分页
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->article_m->total($where);
        $config['base_url']         = base_url('manage/Article/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $articles           = $this->article_m->select($where, $limit, $start);
        if ($articles) {
            foreach ($articles as $key => $val) {
                if (!empty($val['category'])) {
                    $category                   = $this->articleCategory_m->find($val['category']);
                    $articles[$key]['category'] = $category['category_name'];
                } else {
                    $articles[$key]['category'] = "--";
                }
            }
        }
        $data['search']   = $search;
        $data['articles'] = $articles;
        $this->load->view('article/index', $data);
    }

    public function add()
    {
        $data['pic_path'] = "[]";
        $data['pic_con']  = json_encode([]);
        $data['category'] = $this->articleCategory_m->select(['deleted' => 0]);
        $this->load->view('article/form', $data);
    }

    public function edit($article_id = null)
    {
        $article = $this->article_m->find($article_id);
        if (!$article) {
            show_message('参数错误！', site_url(X_URL . 'Article/index'));
        }
        $del_url = base_url("index.php/Upload/delete_img?url=" . $article['img']);;
        $pic_con[]          = [
            'caption' => "产品图",
            'size'    => 576237,
            'width'   => '120px',
            'url'     => $del_url,
            "key"     => $article['id'],
        ];
        $data['pic_path']   = "['" . $article['img'] . "']";
        $data['pic_con']    = json_encode($pic_con);
        $data['article']    = $article;
        $data['article_id'] = $article['id'];
        $data['category']   = $this->articleCategory_m->select(['deleted' => 0]);
        $this->load->view('article/form', $data);
    }

    public function del($article_id)
    {
        if ($article_id) {
            if ($this->article_m->delete_by(['id' => $article_id])) {
                show_message('恭喜，删除成功', site_url(X_URL . 'Article/index'), 1);
            } else {
                show_message('参数错误', site_url(X_URL . 'Article/index'));
            }
        } else {
            show_message('参数错误', site_url(X_URL . 'Article/index'));
        }
    }

    public function update()
    {

        $post          = $this->input->post();
        $article_title = isset($post['title']) ? htmlspecialchars($post['title'], ENT_QUOTES) : false;
        $article_id    = isset($post['article_id']) ? intval($post['article_id']) : false;
        if (empty($post['category'])) {
            show_message('请选择所属类型', site_url(X_URL . 'Article/add'));
        }
        $article = [
            'title'    => $article_title,
            'category' => $post['category'] ?? "",
            'author'   => $post['author'] ?? "",
            'img'      => $post['img'] ?? "",
            'content'  => $post['content'] ?? "",
            'content2'  => $post['content2'] ?? "",
            'keyword'  => $post['keyword'] ?? "",
            'describe' => $post['describe'] ?? "",
            'is_hot'   => $post['is_hot'] ?? 0,
        ];
        if (!$article_id) {
            // 新增
            $article['create_time'] = date("Y-m-d H:i:s", time());
            $articles               = $this->article_m->insert($article);
            if ($articles) {
                show_message('新增产品成功', site_url(X_URL . 'Article/index'), 1);
            } else {
                show_message('新增产品失败', site_url(X_URL . 'Article/add'));
            }
        } else {
            // 修改产品信息
            $affected_rows = $this->article_m->update(['id' => $article_id], $article);
            if ($affected_rows) {
                show_message('修改产品成功', site_url(X_URL . 'Article/index'), 1);
            } else {
                show_message('修改产品失败', site_url(X_URL . 'Article/edit'));
            }
        }
    }

    public function trans()
    {

        $this->load->model('news_m');

        for ($i = 1; $i <= 8; $i++) {

            $start = ($i - 1) * 100;
            $data  = $this->news_m->select("", 100, $start);

            if ($data) {
                foreach ($data as $key => $val) {

                    $article = [
                        'title'       => $val['news_title'],
                        'category'    => 10,
                        'author'      => $val['news_author'] ?? "",
                        'img'         => $val['news_img'] ?? "",
                        'content'     => $val['news_content'] ?? "",
                        'qq'          => $val['qq'] ?? "",
                        'sale_price'  => $val['sale_price'] ?? "",
                        'keyword'     => $val['keywords'] ?? "",
                        'describe'    => $val['description'] ?? "",
                        'create_time' => date("Y-m-d H:i:s", $val['date']),
                    ];

                    $this->article_m->insert($article);
                }
            }
        }

        echo "ok";
    }

    /**
     * 生成网站地图
     * @return bool
     */
    public function sitemap()
    {

        // 删除原来的网站地图
        if (file_exists("sitemap.xml")) {
            unlink("sitemap.xml");
        }
        $this->load->library('map');
        // 开始生成新的网站地图
        // 网站首页
        $this->map->AddItem($this->config->item('www_url'), 0);
        $this->map->AddItem($this->config->item('www_url') . "/", 0);
        $this->map->AddItem($this->config->item('www_url') . "/company.html", 0);
        $this->map->AddItem($this->config->item('www_url') . "/service.html", 0);
        $this->map->AddItem($this->config->item('www_url') . "/price.html", 0);
        $this->map->AddItem($this->config->item('www_url') . "/news.html", 0);
        $this->map->AddItem($this->config->item('www_url') . "/contact.html", 0);
        // 网站栏目
        $this->load->model('articleCategory_m');
        $category = $this->articleCategory_m->select();
        if (!empty($category)) {
            foreach ($category as $v) {
                $this->map->AddItem($this->config->item('www_url') . '/service/' . $v['id'] . ".html", 1);
            }
        }
        // 网站产品
        $this->load->model('article_m');
        $list = $this->article_m->select(['deleted' => 0], 1000, 0, 'id');
        if (!empty($list)) {
            foreach ($list as $v) {
                $this->map->AddItem($this->config->item('www_url') . '/news/' . $v['id'] . ".html", 1);
            }
        }
        $this->map->SaveToFile('sitemap.xml');

        if (file_exists("sitemap.xml")) {
            show_message('网站地图生成成功', site_url(X_URL . 'Home/index'), 1);
        } else {
            show_message('网站地图生成失败', site_url(X_URL . 'Home/index'));
        }
    }
}

